package com.mz.mattress.client.util;

import com.mz.mattress.api.util.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class RandomGenerator {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final Random random = new Random();

    public static int nextInt(int min, int max) {
        return ThreadLocalRandom.current().nextInt(max - min) + min;
    }

    public static int nextSex() {
        int dd = random.nextInt(10);
        return dd > 3 ? 2 : 1;
    }

    public static int nextBirthday() {
        int dd = random.nextInt(10);
        return dd > 3 ? 2 : 1;
    }

    public static int nextWeight() {
        // 生成45到75之间的随机整数
        return 45 + random.nextInt(31); // 75-45+1=31
    }

    //生成商品ID
    public static int nextGoodsId() {
        int rand = random.nextInt(100);
        if (rand < 60) {
            return 2;
        } else if (rand < 70) {
            return 3;
        } else if (rand < 90) {
            return 4;
        }
        return 5;
    }

    //生成设备ID
    public static int nextDeviceId() {
        //0-163
        int rand = random.nextInt(164);
        return rand + 1;
    }

    public static int nextTotal() {
        return 60 + random.nextInt(30);
    }

    public static int nextPayTotal() {
        return 50 + random.nextInt(30);
    }

    public static boolean nextIsCancelOrder() {
        int rand = random.nextInt(100);
        if (rand < 25) {
            return true;
        }
        return false;
    }

    /**
     * 生成符合要求的随机时间
     * @param date 基础日期
     * @return 当天符合要求的时间
     */
    private static Date generateValidTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        // 工作时间 9:00-18:00
//        int hour = 9 + random.nextInt(8); // 9-17

        // 设置小时和分钟
        int hour;
        double rand = random.nextDouble();
        // 权重分配：工作时间(9-18)占60%，其他白天时间(6-9,18-24)占40%
        if (rand < 0.6) {
            // 工作时间 9:00-18:00
            hour = 9 + random.nextInt(9); // 9-17
        } else {
            // 其他白天时间 6:00-9:00 或 18:00-24:00
            if (random.nextBoolean()) {
                hour = 6 + random.nextInt(3); // 6-8
            } else {
                hour = 18 + random.nextInt(6); // 18-23
            }
        }

        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, random.nextInt(60));
        cal.set(Calendar.SECOND, random.nextInt(60));
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    public static String nextTime(String dateStr) {

        Date date = DateUtil.toDate(dateStr);

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        // 工作时间 9:00-18:00
        int hour = 18 + 24 + random.nextInt(7);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, random.nextInt(60));
        cal.set(Calendar.SECOND, random.nextInt(60));
        cal.set(Calendar.MILLISECOND, 0);

        return sdf.format(cal.getTime());

    }

    /**
     * 生成排序后的随机时间列表
     * @param start 开始日期
     * @param end   结束日期
     * @param count 生成数量
     * @return 排序后的时间字符串列表
     */
    public static List<String> generateSortedRandomTimes(Date start, Date end, int count) {
        List<Date> dates = new ArrayList<>();
        long startMillis = start.getTime();
        long endMillis = end.getTime();

        // 生成随机日期（不考虑时间部分）
        for (int i = 0; i < count; i++) {
            long randomMillis = startMillis + (long) (random.nextDouble() * (endMillis - startMillis));
            Date randomDate = new Date(randomMillis);
            dates.add(generateValidTime(randomDate));
        }

        // 按时间排序
        Collections.sort(dates);

        // 转换为字符串
        List<String> result = new ArrayList<>();
        for (Date date : dates) {
            result.add(sdf.format(date));
        }

        return result;
    }

    public static List<String> generateSortedOrderDate(Date day, int count) {

        List<Date> dates = new ArrayList<>();

        // 生成随机日期（不考虑时间部分）
        for (int i = 0; i < count; i++) {
            dates.add(genOrderDate(day));
        }

        // 按时间排序
        Collections.sort(dates);

        // 转换为字符串
        List<String> result = new ArrayList<>();
        for (Date date : dates) {
            result.add(sdf.format(date));
        }

        return result;
    }

    /**
     * 上午时段（9:00-12:00）：约占15%
     * 午间高峰（12:00-14:30）：约占25%
     * 下午平峰（14:30-17:30）：约占20%
     * 晚间高峰（17:30-20:30）：约占40%
     * @param date
     * @return
     */
    private static Date genOrderDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        // 设置小时和分钟
        int hour;
        double rand = random.nextDouble();
        if (rand < 0.15) {
            // 9:00-12:00
            hour = 9 + random.nextInt(3);
        } else if (rand < 0.4) {
            // 12:00-14:30
            hour = 12 + random.nextInt(3);
        } else if (rand < 0.6) {
            // 14:30-17:30
            hour = 15 + random.nextInt(3);
        } else {
            // 17:30-20:30
            hour = 18 + random.nextInt(3);
        }

        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, random.nextInt(60));
        cal.set(Calendar.SECOND, random.nextInt(60));
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    public static void main(String[] args) throws Exception {
        // 设置时间范围（这里设置为过去1年到当前时间）
//        Calendar calendar = Calendar.getInstance();
//        Date end = calendar.getTime(); // 当前时间
//        calendar.add(Calendar.YEAR, -1); // 1年前
//        Date start = calendar.getTime();

        String startDate = "2025-02-28 11:13:34";
        Date start = DateUtil.toDateTime(startDate);

        String dateString = DateUtil.toDateTimeStr(start);

        System.out.println(dateString);


//        List<String> list = generateSortedOrderDate(start, 200);
//
//        for (String ss : list) {
//            System.out.println(ss);
//        }

//        for (int i = 0; i < 100; i++) {
//            System.out.println(random.nextDouble());
////            System.out.println(random.nextInt(2));
//        }


//        String startDate = "2025-02-28";
//        String endDate = "2025-03-31";
//
//        Date start = DateUtil.toDate(startDate);
//        Date end = DateUtil.toDate(endDate);
//
//        // 生成20个按时间排序的随机时间
//        List<String> sortedTimes = generateSortedRandomTimes(start, end, 100);
//
//        // 输出结果
//        System.out.println("按时间从早到晚排序的随机时间（白天6-24点，工作时间占比高）：");
//        for (String time : sortedTimes) {
//            System.out.println(time);
//        }
    }
}
